<template>
  <div>
    <el-input
      placeholder="输入关键字进行过滤"
      v-model="filterText">
    </el-input>
    <el-tree
      class="dep-tree"
      ref="deptree"
      :data="$store.state.common.depData"
      node-key="id"
      :expand-on-click-node="false"
      @node-click="handleClickTree"
      default-expand-all
      :filter-node-method="filterNode"
      :props="defaultProps">
    </el-tree>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      filterText: ''
    }
  },
  watch: {
    filterText(val) {
      this.$refs.deptree.filter(val)
    }
  },
  mounted() {
    this.getDepTree()
  },
  methods: {
    ...mapActions('common', ['getDepTree']),
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    handleClickTree(data) {
      this.$emit('depId', data.id)
    }
  }
}
</script>

<style lang="stylus" scoped>
  .dep-tree
    margin-top 10px
</style>
